package au.com.betaversion.aws;

import java.io.File;

import org.springframework.stereotype.Component;

import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.MultiObjectDeleteException;
import com.amazonaws.services.s3.model.PutObjectRequest;

/**
 * 
 * @author Maciej Radochonski
 *
 */
@Component
public class PhotoStorageS3 implements PhotoStorage {

	private String BUCKET_NAME = "planespotter";
	
	private AmazonS3Client amazonS3Client;
	
	public PhotoStorageS3() {
		this.amazonS3Client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
	}

	/**
	 * 
	 * @param fileName
	 * @param file
	 * @return
	 */
	public boolean uploadFile(String fileName, File file) {
		PutObjectRequest putObjectRequest = new PutObjectRequest(this.BUCKET_NAME, fileName, file);
		putObjectRequest.setCannedAcl(CannedAccessControlList.PublicRead);
		if(this.amazonS3Client.putObject(putObjectRequest) != null)
			return true;
		return false;
	}
	
	/**
	 * 
	 * @param fileName
	 * @return
	 */
	public boolean deleteFile(String fileName) {
		DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(BUCKET_NAME, fileName);
		try {
			this.amazonS3Client.deleteObject(deleteObjectRequest);
			return true;
		} catch(MultiObjectDeleteException e) {
			e.printStackTrace();
			return false;
		}
	}

}
